<?php

class User_pointController extends BaseController
{
    public function listAction() {
        if(!$userInfo = self::model('user')->findById($this->user_info['uid'])){
            Response::ajax(USER_NO_LOGIN, '用户信息获取失败');
        }
        $page = intval(Request::get('page', 1));
        $pageSize = intval(Request::get('pageSize', 20));
        $type = intval(Request::get('type', 0));
        $year = intval(Request::get('year'), date('Y'));
        $month = intval(Request::get('month'), date('m'));
        $where = $value = [];
        $where[] = 'uid=?';
        $value[] = $userInfo['uid'];
        if($type) {
            $where[] = 'type=?';
            $value[] = $type;
        }
        if($year) {
            $where[] = 'year=?';
            $value[] = $year;
        }
        if($month) {
            $where[] = 'month=?';
            $value[] = $month;
        }
        $list = self::model('user_point')->getList($where, $value, $page, $pageSize);
        $list['totalPoints'] = intval($userInfo['points']);
        Response::ajax(200, 'success', $list);
    }
    
    public function orderListAction() {
        $page = intval(Request::get('page', 1));
        $pageSize = intval(Request::get('pageSize', 20));
        $where = $value = [];
        $where[] = 'uid=?';
        $value[] = $this->user_info['uid'];
        Response::ajax(200, 'success', 
                self::model('point_order')->getList($where, $value, $page, $pageSize));
    }
    
    public function orderInfoAction() {
        $orderId = intval(Request::getParams('orderId'));
        $orderInfo = self::model('point_order')->getOne('id=?', [$orderId]);
        if(!$orderInfo||$orderInfo['uid']!=$this->user_info['uid']) {
            Response::ajax(NORMAL_ERROR, '参数错误,订单查看失败');
        }
        Response::ajax(200, 'success', self::model('point_order')->formate($orderInfo));
    }
}